﻿
using System;

using Irony.Compiler;

namespace IronLua.Ast {

	public class UnaryExpression : Expression {

		readonly Expression expression;
		readonly UnaryOperatorType op;

		public Expression Expression {
			get { return expression; }
		}

		public UnaryOperatorType Operator {
			get { return op; }
		}

		public override LuaNodeType NodeType {
			get { return LuaNodeType.UnaryExpression; }
		}

		public UnaryExpression (CompilerContext context, NonTerminal nonTerminal, SourceLocation location, AstNodeList childNodes)
			: base (context, nonTerminal, location, childNodes)
		{
			op = GetOperator (ChildNodes [0].Element.Name);
			expression = ChildNodes [1].ToExpression ();
		}

		static UnaryOperatorType GetOperator (string op)
		{
			switch (op) {
			case "-":
				return UnaryOperatorType.Negation;
			case "not":
				return UnaryOperatorType.Not;
			case "#":
				return UnaryOperatorType.Length;
			}

			throw new InvalidOperationException ();
		}

		public override void Accept (ILuaAstVisitor visitor)
		{
			visitor.VisitUnaryExpression (this);
		}
	}
}
